package com.xckx.VisioViewer;

import java.awt.*;

public class ThreadMonitor extends Thread {
  TransSteps transPanel = null;
  MainFrame  frame      = null;
  Thread     thread;
  String     fileName   = null;

  ThreadMonitor(TransSteps padaptee, Thread pthread) {
    this.transPanel = padaptee;
    this.thread     = pthread;
    this.frame      = null;
  }

  ThreadMonitor(MainFrame pframe, Thread pthread, String fn) {
    this.transPanel = null;
    this.frame      = pframe;
    this.thread     = pthread;
    this.fileName   = fn;
  }

  public void run() {
    while (true) {
      if (!thread.isAlive() || thread.isInterrupted()) {
        this.cleanHouse();
        break;
      }
      try {
        this.sleep(200);
      } catch (Exception e) {
        this.cleanHouse();
        break;
      }
    }
  }

  private void cleanHouse() {
    thread.stop();
    if (frame == null)
      transPanel.processStep(6);
    else
    {
      frame.addDoc(fileName);
    }
    this.stop();
    this.destroy();
  }
}
